<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
</head>
<body>
    <div class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin" style="padding: 20px 30px 0 0;">
        <input type="hidden" name="id" th:if="${flight}" th:value="${flight.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">航班号</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.flightNo}:''" type="text" name="flightNo" lay-verify="required" placeholder="请输入航班号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">航空公司</label>
            <div class="layui-input-inline">
                <select name="companyId" id="companyId" lay-filter="brickType">
                    <option th:each="company1 : ${companyList}" th:value="${company1.id}" th:text="${company1.name}" th:selected="${flight != null and flight.models.company.id == company1.id}"> </option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">机型</label>
            <div class="layui-input-inline">
                <select name="modelsId" id="modelsId">
                    <option th:each="models1 : ${modelsList}" th:value="${models1.id}" th:text="${models1.modelsNo}" th:selected="${flight != null and flight.models.id == models1.id}"> </option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出发地</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.startPlace}:''" type="text" name="startPlace"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">到达地</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.endPlace}:''" type="text" name="endPlace" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出发机场</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.startAirport}:''" type="text" name="startAirport"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">到达机场</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.endAirport}:''" type="text" name="endAirport"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出发时间</label>
            <div class="layui-input-inline">
                <input type="text" name="startDate1" id="startDate1" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" autocomplete="off" class="layui-input">
                <input id="startDate" type="hidden" th:value="${flight != null}?${flight.startDate}:''" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">到达时间</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.endDate}:''" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" type="text" name="endDate1" id="endDate1"  autocomplete="off" class="layui-input">
                <input id="endDate" type="hidden" th:value="${flight != null}?${flight.endDate}:''" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">头等舱价格</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.firstPrice}:''" type="text" name="firstPrice"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公务舱价格</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.secondPrice}:''" type="text" name="secondPrice"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">经济舱价格</label>
            <div class="layui-input-inline">
                <input th:value="${flight != null}?${flight.lastPrice}:''" type="text" name="lastPrice"   autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button th:text="${flight != null}?'立即修改':'立即添加'" class="layui-btn" lay-submit="" lay-filter="addadmin">立即添加</button>
                <button th:if="${flight == null}" type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>

    <script th:src="@{/layuiadmin/layui/layui.js}"></script>
    <script th:src="@{/js/jquery-2.1.1.min.js}"></script>
    <script th:src="@{/My97DatePicker/WdatePicker.js}"></script>
    <script>

        layui.use(['form','jquery'], function () {
            var form = layui.form;
            var $ = layui.jquery;
            form.on('submit(addadmin)',function (data) {
                $.ajax({
                    url:'/manager/saveFlight',
                    data:data.field,
                    dataType:'json',
                    type:'post',
                    success:function (data) {
                        if (data.success){
                            layer.msg(data.message);
                            layer.alert(data.message,function(){
                                window.parent.location.reload();//刷新父页面
                                parent.layer.close(index);//关闭弹出层
                            });
                        }else{
                            layer.msg(data.message);
                        }
                    }
                });
                return false;
            })

            //重置
            $("#reset").click(function () {
                $("input").val("");
            })

            form.on('select(brickType)', function(data){
                var companyId=data.value;
                $.ajax({
                    url:"/manager/findByCompanyId",
                    type:"post",
                    data:{"companyId":companyId},
                    dataType:"json",
                    success:function (data) {
                        var rows=data.rows;
                        var html="";
                        for(var i=0;i<rows.length;i++){
                            html+="<option value='"+rows[i].id+"'>"+rows[i].modelsNo+"</option>"
                        }
                        $("#modelsId").html(html);
                        var form = layui.form
                        form.render();
                    }
                });
            });

        });

        $(function () {
            var startDate=$("#startDate").val();
            if(startDate!=""&&startDate!=null){
                var startDate1=getDate(startDate);
                $("#startDate1").val(startDate1);
            }

            var endDate=$("#endDate").val();
            if(endDate!=""&&endDate!=null){
                var endDate1=getDate(endDate);
                $("#endDate1").val(endDate1);
            }
        });

        function getDate(val){
            var date = new Date(val);
            var year = date.getFullYear().toString();
            var month = (date.getMonth() + 1);
            var day = date.getDate().toString();
            var hour = date.getHours().toString();
            var minutes = date.getMinutes().toString();
            var seconds = date.getSeconds().toString();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (seconds < 10) {
                seconds = "0" + seconds;
            }
            return ""+year + "-" + month + "-" + day + " " + hour + ":" + minutes +"";
        }

    </script>
</body>
</html>